﻿using Engine;
using Engine.Graphics;
using Game;
using GameEntitySystem;

namespace SCIENEW
{
	public class TransferDevice : SixDirectionModelDevice, IElectricElementDevice
	{
        public BlockMesh m_displayBlockMesh = new();

        public TransferDevice() : base("TransferDevice", "Models/Transfer", "Transfer") {
            m_displayBlockMesh.AppendBlockMesh(m_blockMeshesByFace[0]);
            m_displayBlockMesh.TransformPositions(Matrix.CreateTranslation(-0.5f, -0.5f, -0.5f));
        }

		public override BlockPlacementData GetPlacementValue(SubsystemTerrain subsystemTerrain, ComponentMiner componentMiner, int value, TerrainRaycastResult raycastResult)
		{
			return new BlockPlacementData { Value = DevicesBlockManager.GetDeviceValue(Id, DevicesUtils.GetDirectionXYZ(componentMiner, true)), CellFace = raycastResult.CellFace };
		}
		public override DeviceBehavior[] GetBehaviors(Project project) => [new TransferDeviceBehavior(project)];

		public ElectricElement CreateElectricElement(SubsystemElectricity subsystemElectricity, int value, int x, int y, int z) => new TransferElectricElement(subsystemElectricity, new Point3(x, y, z));

		public ElectricConnectorType? GetConnectorType(SubsystemTerrain terrain, int value, int face, int connectorFace, int x, int y, int z) => ElectricConnectorType.Input;

		public int GetConnectionMask(int value) => int.MaxValue;

		public override int GetDisplayOrder(int value) => base.GetDisplayOrder(value) - 2;

        public override int GetFaceTextureSlot(int face, int value) => 170;

        public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData) {
            BlocksManager.DrawMeshBlock(
                primitivesRenderer,
                m_displayBlockMesh,
                IndustrialModLoader.IETexture,
                color,
                size,
                ref matrix,
                environmentData
            );
        }

        public override bool IsFaceTransparent(SubsystemTerrain subsystemTerrain, int face, int value) => true;

        public override bool IsTransparent_(int value) => true;
    }
}